package com.dm.logistics.loginmanage.service.impl;

import com.dm.logistics.common.util.EncryptionUtil;
import com.dm.logistics.loginmanage.mapper.LoginMapper;
import com.dm.logistics.loginmanage.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginMapper loginMapper;

    @Override
    public Map<String, Object> queryUserInfosByUsername(Map<String, Object> param, HttpServletRequest request) {
        Map<String,Object> userMap = loginMapper.queryUserInfosByUsernameMapper(param);
        Map<String,Object> statusMap = new HashMap<String,Object>();
        String inputPwd = param.get("password")!=null?param.get("password").toString():null;
        inputPwd = EncryptionUtil.getSha1(inputPwd);
        inputPwd = inputPwd.toUpperCase();
        String queryPwd = null;
        String userName = null;
        if(userMap!=null){
            queryPwd = userMap.get("Password")!=null?userMap.get("Password").toString():null;
            userName = userMap.get("Name")!=null?userMap.get("Name").toString():null;
        }
        if(userMap==null){
            //0代表没有当前用户
            statusMap.put("status","0");
        }else{
            if(inputPwd.equals(queryPwd)){

                //1代表有当前用户并且密码输入正确
                String store = userMap.get("Store").toString();
                String userno = param.get("username")!=null?param.get("username").toString():null;
                HttpSession session = request.getSession();
                session.setAttribute("userno",userno);
                session.setAttribute("username",userName);
                session.setAttribute("store",store);
                statusMap.put("status",1);
                statusMap.put("store",store);
                statusMap.put("userno",userno);
                statusMap.put("username",userName);
            }else{
                //2代表有当前用户但是密码输入错误
                statusMap.put("status",2);
            }
        }

        return statusMap;
    }

    @Override
    public List<Map<String, Object>> queryZDStore(Map<String, Object> param) {
        List<Map<String,Object>> resultList = loginMapper.queryZDStoreMapper(param);
        return resultList;
    }

    /**
         * @Description 设置下拉框参数
         * @Author zhangxue
         * @Date 18:06 2018/5/24
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> saveSettings(Map<String, Object> map) {
        List<Map<String,Object>> setList = new ArrayList<Map<String, Object>>();
        String equtype = map.get("equtype").toString();
        List<String> EquType = (List<String>) map.get("EquType");
        if(EquType!=null){
            for(int i=0;i<EquType.size();i++){
                Map<String,Object> tempMap = new HashMap<String,Object>();
                tempMap.put("SetKey",equtype);
                tempMap.put("SetValue",EquType.get(i));
                setList.add(tempMap);
            }
        }
        /*String consetype = map.get("consetype").toString();
        List<String> ConseType = (List<String>) map.get("ConseType");
        if(ConseType!=null){
            for(int i=0;i<ConseType.size();i++){
                Map<String,Object> tempMap = new HashMap<String,Object>();
                tempMap.put("SetKey",consetype);
                tempMap.put("SetValue",ConseType.get(i));
                setList.add(tempMap);
            }
        }
        String purchasetype = map.get("purchasetype").toString();
        List<String> PurchaseType = (List<String>) map.get("PurchaseType");
        if(PurchaseType!=null){
            for(int i=0;i<PurchaseType.size();i++){
                Map<String,Object> tempMap = new HashMap<String,Object>();
                tempMap.put("SetKey",purchasetype);
                tempMap.put("SetValue",PurchaseType.get(i));
                setList.add(tempMap);
            }
        }
        String equstatus = map.get("equstatus").toString();
        List<String> EquStatus = (List<String>) map.get("EquStatus");
        if (EquStatus!=null){
            for(int i=0;i<EquStatus.size();i++){
                Map<String,Object> tempMap = new HashMap<String,Object>();
                tempMap.put("SetKey",equstatus);
                tempMap.put("SetValue",EquStatus.get(i));
                setList.add(tempMap);
            }
        }*/
        int j = loginMapper.deleteSettingsMapper();
        int i = loginMapper.saveSettingsMapper(setList);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i>=0){
            resultMap.put("status","1");
        }else{
            resultMap.put("status","0");
        }
        return resultMap;
    }

    /**
         * @Description 查询设备类别下拉框信息
         * @Author zhangxue
         * @Date 15:27 2018/5/25
         * @Param
         * @retrun
         **/
    @Override
    public List<Map<String, Object>> queryEquType(Map<String, Object> equTypeMap) {
        List<Map<String,Object>> resultList = loginMapper.queryEquTypeMapper(equTypeMap);
        return resultList;
    }
}
